package com.stars.easyms.monitor.impl;

import com.stars.easyms.monitor.handler.EasyMsRequestHandler;
import com.stars.easyms.monitor.MonitorFunc;
import com.stars.easyms.monitor.register.EasyMsMonitorRegister;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * <p>className: EasyMsMonitorRequestHandler</p>
 * <p>description: EasyMs监控请求处理类</p>
 *
 * @author guoguifang
 * @date 2019/11/20 11:38
 * @since 1.4.0
 */
public final class EasyMsMonitorRequestHandler implements EasyMsRequestHandler {

    @GetMapping("monitor")
    public void toIndex(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.getRequestDispatcher("/webjars/easyms/monitor/monitor.html").forward(request, response);
    }

    @PostMapping("easy-ms/monitor/getInfo")
    @ResponseBody
    public Map<String, Object> getMonitorInfo() {
        Map<String, Object> monitorSummaryMap = new LinkedHashMap<>();
        for (MonitorFunc monitorFunc : EasyMsMonitorRegister.getMonitorFuncList()) {
            monitorSummaryMap.put(monitorFunc.getModuleName(), monitorFunc.getMonitorInfo());
        }
        return monitorSummaryMap;
    }
}
